﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "ElementUIUMG/Public/ElementUIFilletButton.h"
#include "Engine/Font.h"
#include "Widget/SFilletButton.h"


UElementUIFilletButton::UElementUIFilletButton()
{
	Text = FText::FromString("test");
	Radius = 0.5f;
	RadiusSize = FVector2D(30, 30);
	BorderWidth = 0.05;
	BorderColor = FLinearColor::White;
	BorderSharpness = 100;
	BorderOpacity = 1;
	BackgroundColor = FLinearColor::White;
	BackgroundOpacity = 1;
	Disabled = false;
	UseImage = false;
	HaveChild = false;
	TextHAlign = EHorizontalAlignment::HAlign_Center;
	TextVAlign = EVerticalAlignment::VAlign_Center;
	TextColor = FLinearColor::Black;
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> IconMaterial(TEXT("Material'/ElementUI/Material/Common/M_Fillet.M_Fillet'"));
	if (IconMaterial.Succeeded())
	{
		IconBrush.SetResourceObject(IconMaterial.Object);
	}
	if (!IsRunningDedicatedServer())
	{
		// FCompositeFont MyCompFont = FCompositeFont(FName(TEXT("MyRobotoFont")), "filepath/somefont.ttf", EFontHinting::Default, EFontLoadingPolicy::LazyLoad);
		// TSharedPtr<const FCompositeFont> MyInCompositeFont(new FCompositeFont(MyCompFont)); 
		// TextFont = FSlateFontInfo(MyInCompositeFont, 20);
		static ConstructorHelpers::FObjectFinder<UFont> RobotoFontObj(L"Font'/ElementUI/Font/SIMSUN_Font.SIMSUN_Font'");
		// RobotoFontObj.Object->CompositeFont
		if (RobotoFontObj.Succeeded())
		{
			TextFont = FSlateFontInfo(RobotoFontObj.Object, 18, FName("SIMSUN"));
		}
	}
}

void UElementUIFilletButton::SynchronizeProperties()
{
	Super::SynchronizeProperties();

	FilletButtonSlate->SetText(Text);
	FilletButtonSlate->SetTextFont(TextFont);
	FilletButtonSlate->SetTextColorAndOpacity(TextColor);
	FilletButtonSlate->SetHAlign(TextHAlign);
	FilletButtonSlate->SetVAlign(TextVAlign);
	GetDynamicMaterial();
	UpdateBrush();
	SetUseImage(UseImage);
}

void UElementUIFilletButton::ReleaseSlateResources(bool bReleaseChildren)
{
	Super::ReleaseSlateResources(bReleaseChildren);
	FilletButtonSlate.Reset();
}

void UElementUIFilletButton::PostLoad()
{
	Super::PostLoad();

	if ( GetChildrenCount() > 0 )
	{
		if ( UPanelSlot* PanelSlot = GetContentSlot() )
		{
			UElementUISlot* ButtonSlot = Cast<UElementUISlot>(PanelSlot);
			if ( ButtonSlot == NULL )
			{
				ButtonSlot = NewObject<UElementUISlot>(this);
				ButtonSlot->Content = GetContentSlot()->Content;
				ButtonSlot->Content->Slot = ButtonSlot;
				Slots[0] = ButtonSlot;
			}
		}
	}
}

void UElementUIFilletButton::SetText(FText value)
{
	Text = value;
}

void UElementUIFilletButton::SetDisabled(bool value)
{
	Disabled = value;
}

FReply UElementUIFilletButton::SlateHandleClicked()
{
	OnClicked.Broadcast();
	return FReply::Handled();
}

void UElementUIFilletButton::SlateHandleHovered()
{
	OnHovered.Broadcast();
}

void UElementUIFilletButton::SlateHandleUnhovered()
{
	OnUnhovered.Broadcast();
}

UClass* UElementUIFilletButton::GetSlotClass() const
{
	return UElementUISlot::StaticClass();
}

void UElementUIFilletButton::OnSlotAdded(UPanelSlot* InSlot)
{
	if (FilletButtonSlate.IsValid())
	{
		CastChecked<UElementUISlot>(InSlot)->BuildSlot(FilletButtonSlate.ToSharedRef());
		FilletButtonSlate->HideDefaultSlate();
	}
}

void UElementUIFilletButton::OnSlotRemoved(UPanelSlot* InSlot)
{
	if (FilletButtonSlate.IsValid())
	{
		FilletButtonSlate->SetContent(SNullWidget::NullWidget);
		FilletButtonSlate->ResetButton();
		FilletButtonSlate->ShowDefaultSlate();
	}
}

UMaterialInstanceDynamic* UElementUIFilletButton::GetDynamicMaterial()
{
	UMaterialInterface* Material = nullptr;
	UObject* Resource = IconBrush.GetResourceObject();
	Material = Cast<UMaterialInterface>(Resource);
	if (Material)
	{
		UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Material);
		if (!DynamicMaterial)
		{
			// UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__));
			DynamicMaterial = UMaterialInstanceDynamic::Create(Material, this);
			IconBrush.SetResourceObject(DynamicMaterial);
		}
		DynamicMaterial->SetScalarParameterValue("UseImage", UseImage ? 1 : 0);
		DynamicMaterial->SetScalarParameterValue("BackgroundOpacity", BackgroundOpacity);
		DynamicMaterial->SetScalarParameterValue("BorderOpacity", BorderOpacity);
		DynamicMaterial->SetScalarParameterValue("BorderWidth", BorderWidth);
		DynamicMaterial->SetScalarParameterValue("Radius", Radius);
		DynamicMaterial->SetScalarParameterValue("Sharpness", BorderSharpness);
		DynamicMaterial->SetVectorParameterValue("BackgroundColor", BackgroundColor);
		DynamicMaterial->SetVectorParameterValue("BorderColor", BorderColor);
		if (FilletButtonSlate.IsValid())
		{
			FilletButtonSlate->SetBorderImage(&IconBrush);
		}
		return DynamicMaterial;
	}
	return nullptr;
}

void UElementUIFilletButton::SetUseImage(bool value)
{
	UseImage = value;
	if (UseImage)
	{
		IconBrush.DrawAs = ESlateBrushDrawType::Image;
	} else
	{
		IconBrush.DrawAs = ESlateBrushDrawType::Box;
		IconBrush.Margin = FMargin(0.5);
	}
}

void UElementUIFilletButton::UpdateBrush()
{
	IconBrush.SetImageSize(RadiusSize);
	// UMaterialInstanceDynamic* BorderMaterial = GetDynamicMaterial();
	// if (!BorderMaterial) return;
	// if (FilletButtonSlate.IsValid())
	// {
	// 	FilletButtonSlate->SetBorderImage(&IconBrush);
	// }
}

TSharedRef<SWidget> UElementUIFilletButton::RebuildWidget()
{
	FilletButtonSlate = SNew(SFilletButton)
		.Text(Text)
		.TextColor(TextColor)
		.HAlign(TextHAlign)
		.VAlign(TextVAlign)
		.Disabled(Disabled)
		.OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SlateHandleClicked))
		.OnHovered_UObject(this, &UElementUIFilletButton::SlateHandleHovered)
		.OnHovered_UObject(this, &UElementUIFilletButton::SlateHandleUnhovered);

	if (GetChildrenCount() > 0)
	{
		Cast<UElementUISlot>(GetContentSlot())->BuildSlot(FilletButtonSlate.ToSharedRef());
	}

	return FilletButtonSlate.ToSharedRef();
}
